<?php
use yii\helpers\Url;

$web = Url::base();
$scriptUrl = \Yii::$app->request->scriptUrl;

?>

<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">
            <div class="btn-group col-lg-2">
                <a class="btn btn-default " type="button" data-toggle="modal" href="#addModal"><i
                            class="fa fa-plus"></i> 新增管理员</a>
            </div>

            <div class="pull-right">
                <div class="form-group">
                    <div id="searchInputInstitution"></div>

                </div>
                <!--                <div class="form-group">-->
                <!--                    <label class="sr-only" for="search2">关键字</label>-->
                <!--                    <input type="text" class="form-control" id="search_key" placeholder="关键字">-->
                <!--                </div>-->
                <button id="btnSearch" type="button" class="btn btn-primary">
                    <i class="fa fa-search"></i> 搜索
                </button>
            </div>
        </div>
    </div>
</div>

<div class="panel">
    <div class="panel-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-condensed">
                <thead class="nowrap">
                <tr>
                    <th>姓名</th>
                    <th>账号</th>
                    <!--                    <th>所属机构</th>-->
                    <!--                    <th>子机构</th>-->
                    <th>手机号</th>
                    <th>是否启用</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <?php
                use app\models\Manager;

                foreach ($items as $item) {
                    ?>
                    <tr>
                        <td><?= $item['name'] ?></td>
                        <td><?= $item['account'] ?></td>
                        <!--                        <td>--><?//= $item['institution_name'] ?><!--</td>-->
                        <!--                        <td>-->
                        <!--                            --><?//= $department = app\models\Department::getDepartmentByManagerId($item['id'])['name'] ?>
                        <!--                        </td>-->
                        <td><?= $item['phone'] ?></td>
                        <td>
                            <?php
                            if ($item['is_enabled'] == 1) {
                                ?>
                                <span class="label label-success">启用</span>
                                <?php
                            } else {
                                ?>
                                <span class="label label-danger">禁用</span>
                                <?php
                            }
                            ?>
                        </td>
                        <td>
                            <div class="btn-group">
                                <a class="btn btn-link" href="javascript:void(0)" data-toggle="modal"
                                   data-target="#modifyPasswordModal"
                                   onclick="updatePassword('<?= $item['id'] ?>','<?= $item['account'] ?>')">重置密码</a>
                                <?php
                                if ($item['id'] != '0') {
                                    ?>
                                    <a class="btn btn-link" data-toggle="modal" data-manager-id="<?= $item['id'] ?>"
                                       href="#detailModal">编辑</a>
                                    <?php
                                }
                                ?>
                            </div>
                        </td>
                    </tr>
                    <?php
                }
                ?>
                </tbody>
            </table>
        </div>
        <div class="pull-left">
            <?= \app\widgets\LinkPagerExt::widget(['pagination' => $pagination]) ?>
        </div>
    </div>
</div>

<div aria-hidden="true" role="dialog" tabindex="-1" id="detailModal" class="modal fade">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">管理员详情</h4>
            </div>
            <div id="detail_content">
                正在加载数据....
            </div>
        </div>
    </div>
</div>

<div aria-hidden="true" role="dialog" tabindex="-1" id="addModal" class="modal fade">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">新增管理员</h4>
            </div>
            <div class="modal-body">
                <div class="form-horizontal">
                    <div class="form-group">
                        <label class="col-lg-3 col-sm-3 control-label">机构:</label>
                        <div class="col-lg-9">
                            <div id="inputInstitution"></div>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-lg-3 col-sm-3 control-label">类型：</label>
                        <div class="btn-group col-lg-9">
                            <button type="submit" class="btn btn-info" id="insManager" onclick="changeType(1)"
                                    disabled="disabled">机构管理员
                            </button>
                            <button type="submit" class="btn btn-default" id="depManager" onclick="changeType(2)">子机构管理员
                            </button>
                        </div>
                    </div>
                    <div class="form-group" id="subIns" style="display: none">
                        <label class="col-lg-3 col-sm-3 control-label">子机构:</label>
                        <div class="col-lg-9" id="department_div">
                            <div id="inputdepartment"></div>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-lg-3 col-sm-3 control-label">姓名:</label>
                        <div class="col-lg-9">
                            <input type="text" class="form-control" id="inputName"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 col-sm-3 control-label">账号:</label>
                        <div class="col-lg-9">
                            <input type="text" class="form-control" id="inputAccount"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 col-sm-3 control-label">密码:</label>
                        <div class="col-lg-9">
                            <input type="password" class="form-control" id="inputPassword"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 col-sm-3 control-label">手机号码:</label>
                        <div class="col-lg-9">
                            <input type="tel" class="form-control" id="inputPhone"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 col-sm-3 control-label">是否启用:</label>
                        <div class="col-lg-9">
                            <select class="form-control" id="inputIsEnabled">
                                <option value="1">启用</option>
                                <option value="0">禁用</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label col-lg-3">备注:</label>
                        <div class="col-lg-9">
                            <textarea rows="4" class="form-control" id="inputRemark"></textarea>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="panel">
                            <div class="panel-heading">
                                角色配置
                            </div>
                            <div class="panel-body" id="roleins">
                                <?php
                                //                                $isSystemAdmin = \app\models\Permission::isSystemAdmin();
                                foreach ($roleList as $role) {
//                                    if (!$isSystemAdmin && $role['is_system'] == 1 || $role['is_department'] == 1) {
//                                        continue;
//                                    }
                                    ?>
                                    <div class="square-blue form-inline icheck col-lg-4">
                                        <div class="checkbox popovers" data-trigger="hover" data-placement="top"
                                             data-original-title="<?= $role['name'] ?>"
                                             data-content="<?= $role['description'] ?>">
                                            <input type="checkbox" name="role" value="<?= $role['id'] ?>">
                                            <label><?= $role['name'] ?> </label>
                                        </div>
                                    </div>
                                    <?php
                                }
                                ?>
                            </div>
                            <div class="panel-body" id="roledep" style="display: none">
                                <?php
                                foreach ($roleList as $role) {
                                    if ($role['is_department'] == 1) {
                                        ?>
                                        <div class="square-blue form-inline icheck col-lg-4">
                                            <div class="checkbox popovers" data-trigger="hover" data-placement="top"
                                                 data-original-title="<?= $role['name'] ?>"
                                                 data-content="<?= $role['description'] ?>">
                                                <input type="checkbox" name="role" value="<?= $role['id'] ?>">
                                                <label><?= $role['name'] ?> </label>
                                            </div>
                                        </div>
                                        <?php
                                    }
                                }
                                ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal">关闭</button>
                <button class="btn btn-primary" id="btnAddManagerOk">确定</button>
            </div>
        </div>
    </div>
</div>


<div aria-hidden="true" role="dialog" tabindex="-1" id="modifyPasswordModal" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">重置密码</h4>
            </div>
            <div class="modal-body">
                <div class="form-horizontal adminex-form">
                    <input type="hidden" id="updateManagerId" class="form-control" value="" readonly>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">账号</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="updateAccount" readonly>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">新密码</label>
                        <div class="col-sm-9">
                            <input type="password" class="form-control" id="newPassword">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">确认新密码</label>
                        <div class="col-sm-9">
                            <input type="password" class="form-control" id="confirmPassword">
                            <label id='alertPassword' style="display: none;">
                                <span style="color: red">与新密码输入不一致，请修改。</span>
                            </label>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" id="modifyPasswordModalConfirm">保存</button>
                <button class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<script type="application/javascript">
    var department;
    (function () {

        //20161026修改：由于上级管理员可以操作下级机构管理员 所以让可编辑
//        if ("<?//= Manager::getCurrentManager()['institution_code'] ?>//" == "0") {
//            var rootItem = {};
//            rootItem.name = '系统';
//            rootItem.code = '0';
//            rootItem.children = <?//= json_encode($institutions) ?>//;
//            var institutionSelectSource = [];
//            institutionSelectSource.push(rootItem);
//            var institutionSelect = $('#searchInputInstitution').treeAutocomplete({
//                defaultText: '---- All ----',
//                source: institutionSelectSource
//            }).setValue(<?//= json_encode($institution_code) ?>//);
//
//        } else {
//            var institutionSelect = $('#searchInputInstitution').treeAutocomplete({
//                defaultText: '---- All ----',
//                source:  <?//= json_encode($institutions) ?>
//            }).setValue(<?//= json_encode($institution_code) ?>//);
//        }

        //新增管理员界面的机构加载
//        var institution_this =  <?//= json_encode($institutions) ?>//;

//        var adddefaultText = '--系统--';
//        if ("<?//= Manager::getCurrentManager()['institution_code'] ?>//" != "0") {
//            adddefaultText = '--机构选择--';
//        }
//        var inputInstitution = $('#inputInstitution').treeAutocomplete({
//            defaultText: adddefaultText,
//            source: institution_this,
//            onSelected: function (element) {
//                //新增子机构选择
//                var department_div = $("#department_div");
//                var html = "";
//                html += '<div id="inputdepartment"></div>';
//
//                department_div.html(html);
//
//                var institution_code = element.getValue();
//                var action = "<?//=$scriptUrl?>//" + "/common" + "/department-tree-handler";
//                var data = {};
//                data.institution_code = institution_code;
//                var subdefaultText="--选择子机构--";
//                requestJson(action, data, true, function (responseData) {
//                    if (responseData.status == 0) {
//                        if (responseData.data==''){
//                            subdefaultText="无子机构，请到[机构管理]-[子机构设置]中维护";
//                        }
//                        department= $('#inputdepartment').treeAutocomplete({
//                            defaultText: subdefaultText,
//                            isShowCode: false,
//                            source: responseData.data
//                        });
//                    }
//                });
//
//            }
//        }).setValue(<?//= json_encode($currentInstitutionCode) ?>//);

        <!--        --><?php
        //        if($currentInstitutionCode != ''){
        //        ?>
//        //新增子机构选择
//        var department_div = $("#department_div");
//        var html = "";
//        html += '<div id="inputdepartment"></div>';
//
//        department_div.html(html);
//
//        var institution_code = "<?//=$currentInstitutionCode?>//";
//        var action = "<?//=$scriptUrl?>//" + "/common" + "/department-tree-handler";
//        var data = {};
//        data.institution_code = institution_code;
//        var subdefaultText="--选择子机构--";
//        requestJson(action, data, true, function (responseData) {
//            if (responseData.status == 0) {
//                if (responseData.data==''){
//                    subdefaultText="无子机构，请到[机构管理]-[子机构设置]中维护";
//                }
//                department=   $('#inputdepartment').treeAutocomplete({
//                    defaultText: subdefaultText,
//                    isShowCode: false,
//                    source: responseData.data
//                });
//            }
//        });
//
//        <?php
        //        }
        //        ?>



        $('#btnAddManagerOk').click(function () {

//            if ("<?//= Manager::getCurrentManager()['institution_code'] ?>//" != "0") {
//                if ($('#inputInstitution').val() == '') {
//                    alert("请选择机构");
//                    return;
//                }
//            }
//            if (($("#depManager").prop('disabled') == true) && ($('#inputdepartment').val() == '')) {
//                alert("请选择子机构");
//                return;
//            }
//            var data = {};
//            data.institution_code = $('#inputInstitution').val() == '' ? '0' : $('#inputInstitution').val();
//            if ($("#depManager").prop('disabled') == true) {
//                data.department_id = $('#inputdepartment').val();
//            } else {
//                data.department_id = '';
//            }
//            data.name = $('#inputName').val();
//            data.account = $('#inputAccount').val();
//            data.password = $('#inputPassword').val();
//            data.phone = $('#inputPhone').val();
//            data.is_enabled = $('#inputIsEnabled').val();
//            data.remark = $('#inputRemark').val();
//            data.role_list = [];
//            $("input:checkbox[name=role]:checked").each(function () {
//                data.role_list.push($(this).val());
//            });
//            requestJson('add-manager-handler', data, true, function (responseData) {
//                if (responseData.status == 0) {
//                    $('#addModal').modal('hide');
//                    window.location.reload();
//                } else {
//                    alert(responseData.message);
//                }
//            });
        });

        $('#detailModal').on('show.bs.modal', function (e) {
            $("#detail_content").html('<div class="modal-body">正在加载数据....</div>');
            var managerId = e.relatedTarget.attributes['data-manager-id'].value;
            requestHtml('manager-detail-partview', {manager_id: managerId}, true, function (responseHtml) {
                $("#detail_content").html(responseHtml);
            });
        });

        $('#btnSearch').click(function () {
            var href = window.location.href;
            var institutionCode = $('#searchInputInstitution').val();
            if (institutionCode) {
                href = setUrlParam(href, 'institution_code', institutionCode);
            } else {
                href = removeUrlParam(href, 'institution_code');
            }
            href = setUrlParam(href, 'page', 1);
            window.location.href = href;
        });


        $('#addModal').on('hide.bs.modal', function () {
            $(this).removeData("bs.modal");
            $('#inputName').val("");
            $('#inputAccount').val("");
            $('#inputPassword').val("");
            $('#inputPhone').val("");
            $('#inputRemark').val("");
            $('#inputIsEnabled').val(1);
            //$('#inputInstitution_value').val(0);
            $('input').iCheck('uncheck');

            $("#subIns").css("display", "none");
            //department.setValue('');
            $("#insManager").attr("disabled", true).removeClass('btn-default').addClass('btn-info');
            $("#depManager").attr("disabled", false).removeClass('btn-info').addClass('btn-default');
            $("#roledep").css("display", "none");
            $("#roleins").css("display", "block");

        });

        $('#modifyPasswordModalConfirm').click(function () {
            var data = {};
            data.manageId = $("#updateManagerId").val();
            data.newPassword = $("#newPassword").val();

            if (data.newPassword == $("#confirmPassword").val()) {
                requestJson('modify-password-by-manager-handler', data, true, function (responseData) {
                    if (responseData.status == 0) {
                        window.location.reload();
                    } else {
                        alert(responseData.message);
                    }
                });
            } else {
                $("#alertPassword").css("display", "block");
            }
        });


    })(jQuery);

    //设置模态窗:当用户点击模态框外部时不会关闭、当初始化时不显示模态框。
    $(function () {
        $('#addModal').modal({
            backdrop: 'static', show: false
        });

        $('#detailModal').modal({
            backdrop: 'static', show: false
        });

    });


    function updatePassword(id, account) {
        $('#updateManagerId').val(id);
        $('#updateAccount').val(account);
        $('#newPassword').val('');
        $('#confirmPassword').val('');
    }

    function changeType(i) {
        if (i == 2) {
            $("#subIns").css("display", "block");
            $("#insManager").attr("disabled", false).removeClass('btn-info').addClass('btn-default');
            $("#depManager").attr("disabled", true).removeClass('btn-default').addClass('btn-info');
            $("#roledep").css("display", "block");
            $("#roleins").css("display", "none");

        } else {
            $("#subIns").css("display", "none");
            $("#insManager").attr("disabled", true).removeClass('btn-default').addClass('btn-info');
            $("#depManager").attr("disabled", false).removeClass('btn-info').addClass('btn-default');
            $("#roledep").css("display", "none");
            $("#roleins").css("display", "block");
        }
        $('input').iCheck('uncheck');
    }
</script>
